<?php
defined('_JEXEC') or die('Restricted access');

require_once(JPATH_COMPONENT.DS.'persistance'.DS.'DAOLang.php');
require_once(JPATH_COMPONENT.DS.'domain'.DS.'DTOLang.php');

class DAOLangImpl implements DAOLang{
	
	public function updateLang($con, $dtoLang){

		$sql = "UPDATE #__hizkuntzak_languages SET name_es='$dtoLang->name_es', ";
		$sql = $sql."name_eu='$dtoLang->name_eu', pdf_path='$dtoLang->pdfPath', ";
		$sql = $sql."map_path='$dtoLang->mapPath', ";
		$sql = $sql."rsc_path='$dtoLang->rscPath', audio_path='$dtoLang->audioPath', ";
		$sql = $sql."alf_path='$dtoLang->alfPath', ";
		$sql = $sql."active=$dtoLang->active WHERE iso_code='$dtoLang->iso_code' ";

		$con->setQuery($sql);
		
		if(!$con->query()){
			$con->stderr();
			return false;
		}
		else
			return $dtoLang->iso_code;
	}
	
	public function addLang($conn, $dtoLang){

		$data = new stdClass();
		$data->iso_code = $dtoLang->getIsoCode();
		$data->name_es = $dtoLang->getNameEs();
		$data->name_eu = $dtoLang->getNameEu();
		$data->pdf_path = $dtoLang->getPdfPath();
		$data->map_path = $dtoLang->getMapPath();
		$data->alf_path = $dtoLang->getAlfPath();
		$data->rsc_path = $dtoLang->getRscPath();
		$data->audio_path = $dtoLang->getAudioPath();
		$data->active = $dtoLang->isActive();
		
		if(!$conn->insertObject( '#__hizkuntzak_languages', $data, 'iso_code' )){
			echo $conn->stderr();
			return false;
		}
		else 
			return $data->iso_code;
	    
	}
	
	/*
	 * function to get all DB Langs
	 */
	
	public function getAllLangs($con, $orderby, $active){
		$results = array();
		
		$sql = 'select * from #__hizkuntzak_languages ';
		
		if($active){
			$sql = $sql.'where (active=1)';
		}
		
		$sql = $sql.$orderby;
		
		$con->setQuery($sql);
		
    	$objectList = $con->loadObjectList();

        foreach($objectList as $row) {

        	$lang = new DTOLang();
        	
        	$lang->setIsoCode($row->iso_code);
        	$lang->setNameEs($row->name_es);
        	$lang->setNameEu($row->name_eu);
        	$lang->setPdfPath($row->pdf_path);
        	$lang->setMapPath($row->map_path);
        	$lang->setAlfPath($row->alf_path); 
        	$lang->setRscPath($row->rsc_path);
        	$lang->setAudioPath($row->audio_path); 
        	$lang->setActive($row->active);
            
        	$results[] = $lang;
          } 
		
		return $results;
	}
	
	public function getLang($con, $iso_code){
		$sql = "select * from #__hizkuntzak_languages where (iso_code='$iso_code')";
		
		$con->setQuery($sql);
		
    	$row = $con->loadObject();
    	
    	if($row == null){
    		return null;
    	}
    	$lang = new DTOLang();
    	
    	$lang->setIsoCode($row->iso_code);
        $lang->setNameEs($row->name_es);
        $lang->setNameEu($row->name_eu);
        $lang->setPdfPath($row->pdf_path);
        $lang->setMapPath($row->map_path);
        $lang->setAlfPath($row->alf_path);
        $lang->setRscPath($row->rsc_path);
        $lang->setAudioPath($row->audio_path);  
        $lang->setActive($row->active);
        
        return $lang;
	}
	
	
	
}